﻿using Microsoft.AspNetCore.Http;
using PeiYue.Common;
using PeiYue.IRepository.Base;
using PeiYue.IServices.Common;
using PeiYue.Model.Dto.Common;
using PeiYue.Model.Models.Common;
using PeiYue.Services.BASE;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace PeiYue.Services.Comon
{

    public class ImgResDaoServices : BaseServices<ImgResDao>, IImgResDaoServices
    {

        IBaseRepository<ImgResDao> _dal;
        public ImgResDaoServices(IBaseRepository<ImgResDao> dal)
        {
            this._dal = dal;
            base.BaseDal = dal;
        }

        public async Task<ImgResDao> UploadFile(IFormFile file, ImgResDao model)
        {
            var dic = Appsettings.app(new string[] { "UploadPath" }) + "\\" + model.ImgPath;
            if (!Directory.Exists(dic))
                Directory.CreateDirectory(dic);
            model.ImgPath = "\\" + model.ImgPath + "\\" + new Random().Next(0, 9) + DateTime.Now.ToString("yyMMddhhmmss") + file.FileName;

            //var path = Directory.GetCurrentDirectory() + @"\UploadFile\RouterFee\";
            //foreach (var formFile in files)
            //{
            if (file.Length > 0)
                using (var stream = new FileStream(Appsettings.app(new string[] { "UploadPath" }) + "\\" + model.ImgPath, FileMode.Create))
                    await file.CopyToAsync(stream);

            model.ImgPath = model.ImgPath.Replace(@"\", @"/");
            model.FileName = file.FileName;
            model.id = await _dal.GenId();
            //}
            await _dal.Add(model);
            return model;
        }
    }
}
